package com.stx.boke.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.stx.boke.dao.UserDao;
import com.stx.boke.dao.impl.UserDaoImpl;
import com.stx.boke.entity.User;

/**
* 登录用的servlet撒，又叫登录验证
 * 中间还有其他比较高端的就先不教你了
 * 你先把功能实现了再说
 */
public class CheckLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserDao ud = new UserDaoImpl();   //这里你要调用哪个impl你就new一个它的对象
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");//这两句代码我就不说了撒，知道啥意思撒
		//servlet就是一个页面与后台交互的桥梁
		//所以你得知道页面要你干啥子
		//这里我们就先得到页面穿过来的用户名和密码
		
		String uname = request.getParameter("uname");//""里面填你页面上<input name的值 value="用户名">
		String password = request.getParameter("password");//你懂撒
		
		//下面我们就把获得的值传给方法撒（也就是impl中的方法）
		User user = new User();
		user.setUname(uname);
		user.setPassword(password);
		//现在就用最上面我们new的那个impl对象去调方法撒
		User loginuser = ud.getUser(user);
		//这里方法中返回值是什么就要用什么去接收，getUser方法返回的是一个User对象，你懂撒
		//********************************
		
		
		//下面就进行判断了撒，到底有没有这个娃儿撒
		if(loginuser!=null){    //不为空嘛，就肯定有这个娃儿撒
			HttpSession session = request.getSession();////新建一个session知道啥意思撒
			session.setAttribute("USER", loginuser); //把这个对象放在session中想用了就直接在session中调用撒
			getServletContext().getRequestDispatcher("index.jsp").forward(request, response);
			//response.sendRedirect("index.jsp");//就跳转到登陆上了的页面撒		
		}else{
			request.setAttribute("messages", "用户登录名或密码错误!");
			getServletContext().getRequestDispatcher("/jsp/login.jsp").forward(request, response);
			//response.sendRedirect("/kqblog/jsp/login.jsp");//反之莫得这个娃儿 登个J8 啊，返回登录页面
		}	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);//这个也就不说了！
	}

}
